package
{
	import com.wandian.ApplicationFacade;
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.core.GSaveManager;
	import com.wandian.data.GameData;
	import com.wandian.util.CLZHotkeyManager;
	import com.wandian.util.GraphicsDraw;
	import com.wandian.util.Res;
	import com.wandian.view.layer.Canvas;
	import com.wandian.view.stage.StageGame;
	import com.wandian.view.stage.StageLoading;
	import com.wandian.view.ui.UILogin;

	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.system.ApplicationDomain;
	import flash.system.Capabilities;
	import flash.system.Security;

	[SWF(width="960", height="630", frameRate="24", backgroundColor="0")]
	/**
	 * 德州扑克主程序
	 * @author long
	 */
	public class Texas extends Sprite
	{
		protected static var instance:Texas;
		public var curStage:*;
		private var loginUI:UILogin;
		/**
		 * 小loading界面
		 */
//		public var smallLoading:MovieClip;
		public var facade:ApplicationFacade;
		public var _mcTableView:Canvas=new Canvas();
		public var _mcGameView:Canvas=new Canvas();
		public var _mcWindosView:Canvas=new Canvas();
		public var _mcTopView:Canvas=new Canvas();
		public var _mcTipView:Canvas=new Canvas();
		public var _hotkey:CLZHotkeyManager=CLZHotkeyManager.instance;
		/**
		 * flash版本检测
		 * @default
		 */
		public var Version:String;

		public function Texas()
		{
			super();
			if (instance != null)
				throw new Error("Only one Singleton instance should be instantiated");
			instance=this;
			Security.allowDomain("*");
			initGame();
			instance=this;
		}

		static public function get I():Texas
		{
			if (instance == null)
				instance=new Texas();
			return instance;
		}

		private function initGame():void
		{

			if (stage)
			{
//				stage.align=StageAlign.TOP;
//				stage.scaleMode=StageScaleMode.SHOW_ALL;
//				stage.scaleMode=StageScaleMode.NO_SCALE;
				stage.align=StageAlign.TOP;
				stage.scaleMode=StageScaleMode.SHOW_ALL;
				trace("[Texas]注销o1nStageHandler,click");
				this.stage.addEventListener(MouseEvent.CLICK, this.onStageHandler);
				this.addChild(_mcTableView);
				this.addChild(_mcGameView);
				this.addChild(_mcTopView);
				this.addChild(_mcWindosView);
				this.addChild(_mcTipView);
				var tipMask:Sprite;
				if (GameData.is800)
					tipMask=GraphicsDraw.drawRec(800, 600); //change pixle
				else
					tipMask=GraphicsDraw.drawRec(960, 630); //change pixle	
				this.addChild(tipMask);
				_mcTipView.mask=tipMask;

				this.facade=ApplicationFacade.getInstance();
				this.facade.startup(this);
				stage.stageFocusRect=false;
				_hotkey.init(this.stage);
				this.Version=this.getVersion();
//				stage.displayState=StageDisplayState.FULL_SCREEN;
//				Res.init(ApplicationDomain.currentDomain);

			}
			else
			{
				addEventListener(Event.ADDED_TO_STAGE, initGame);
			}
		}

		private function getVersion():String
		{
			var ver=Capabilities.version;
			var n=ver.split(",", 1);
			ver=n[0] as String;
			return ver.slice(4);
		}

		private function onStageHandler(event:MouseEvent):void
		{
			var texas:DisplayObject=event.target as DisplayObject;
			this.facade.sendNotification(CLZNoticeName.ON_STAGE, {point: new Point(stage.mouseX, stage.mouseY), target: texas});
			return;
		}

		private function _drawMask():Sprite
		{
			var sprite:Sprite=new Sprite
			sprite.graphics.beginFill(0x000000);
			sprite.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight)
			return sprite
		}

		public function setStage(newView:*):void
		{
			addChild(newView)
			if (this.curStage)
			{
				this.curStage.destroy();
				if (this.curStage.parent)
				{
					this.curStage.parent.removeChild(this.curStage);
				}
			}
			this.curStage=newView;
		}

		public function getVisibleWH():Point
		{
			var result:Point;
			if (stage)
			{
				result=new Point(stage.stageWidth, stage.stageHeight);
			}
			else
			{
				trace("获取舞台宽高失败 stage为null");
			}

			return result;
		}

		public function get gameStage():StageGame
		{
			if (this.curStage)
			{
				if (this.curStage is StageGame)
				{
					return this.curStage as StageGame;
				}
			}

			return null;
		}
	}
}
